﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Microsoft.Ccr.Core;
using XcoAppSpaces.Core;
using XcoAppSpaces.Serialization.Binary;
using XcoAppSpaces.Serialization.Json;
using XcoAppSpaces.Transport.Sockets;

namespace test.XcoAppSpaces.Core.Special_Usage_Scenarios
{
	[TestFixture]
	public class testJsonSerialization
	{
		[Test]
		public void TestJson()
		{
			using (XcoAppSpace space1 = XcoAppSpace.ConfigureWithConfigString("tcp.port=8000").UsingService<XcoJsonSerializer>(),
				space2 = XcoAppSpace.ConfigureWithConfigString("tcp.port=8001").UsingService<XcoJsonSerializer>())
			{
				var port = new Port<int>();
				space1.RunWorker(port);

				var remotePort = space2.ConnectWorker<Port<int>>(Environment.MachineName + ":8000");

				remotePort.Post(1);
				Assert.AreEqual(1, port.TestSync(1000));
			}
		}

		[Test]
		public void TestMultipleSerializers()
		{
			using (XcoAppSpace space1 = XcoAppSpace.Configure
					.UsingService<XcoBinarySerializer>().WithName("binary").AsDefault()
					.UsingService<XcoJsonSerializer>().WithName("json")
					.UsingService<XcoTCPTransportService>().OnPort(8000).WithName("tcp1") //should automatically use the default serializer
					.UsingService<XcoTCPTransportService>().OnPort(9000).WithName("tcp2").WithSerializer("json"),
				space2 = XcoAppSpace.Configure
					.UsingService<XcoBinarySerializer>()
					.UsingService<XcoTCPTransportService>().OnPort(8001),
				space3 = XcoAppSpace.Configure
					.UsingService<XcoJsonSerializer>()
					.UsingService<XcoTCPTransportService>().OnPort(9001))
			{
				var port2 = new Port<int>();
				space2.RunWorker(port2);
				var port3 = new Port<int>();
				space3.RunWorker(port3);

				var remotePort2 = space1.ConnectWorker<Port<int>>("tcp1", Environment.MachineName + ":8001", "w1");
				var remotePort3 = space1.ConnectWorker<Port<int>>("tcp2", Environment.MachineName + ":9001", "w2");
				remotePort2.Post(1);
				remotePort3.Post(1);

				Assert.AreEqual(1, port2.TestSync(1000));
				Assert.AreEqual(1, port3.TestSync(1000));
			}
		}

		[Test]
		public void TestPublishSubscribeWithJson()
		{
			using (XcoAppSpace space1 = XcoAppSpace.ConfigureWithConfigString("tcp.port=8000").UsingService<XcoJsonSerializer>(),
				space2 = XcoAppSpace.ConfigureWithConfigString("tcp.port=8001").UsingService<XcoJsonSerializer>())
			{
				var port = new Port<Subscribe<int>>();
				space1.RunWorker(port);

				var remotePort = space2.ConnectWorker<Port<Subscribe<int>>>(Environment.MachineName + ":8000");

				var subscribePort = new Port<int>();
				remotePort.Post(new Subscribe<int>(subscribePort, (i => i == 1)));
				var erg = port.TestSync(1000);
				
				Assert.IsInstanceOf<Port<int>>(erg.EventPort);
				Assert.IsTrue(erg.MatchesFilter(1));
				Assert.IsFalse(erg.MatchesFilter(2));
				Assert.IsFalse(erg.MatchesFilter("x"));
			}
		}
	}
}
